package com.example.mymvvmdemo.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Build;
import android.view.ViewConfiguration;
import android.view.WindowManager;

import java.lang.reflect.Method;

/**
 * 底部导航栏工具类
 */
public class BottomUIUtils {

    private static BottomUIUtils bottomUIUtils;

    public static BottomUIUtils getBottomUIUtils() {
        synchronized (String.class) {
            if (bottomUIUtils == null) {
                synchronized (String.class) {
                    bottomUIUtils = new BottomUIUtils();
                }
            }
        }
        return bottomUIUtils;
    }

    /**
     * 是否隐藏
     */
    public void hideBottom(Activity activity) {
        boolean b = hasNavBar(activity);
        if (b)
            hideBottomUIMenu(activity);
    }

    /**
     * 是否显示
     */
    public void showBottom(Activity activity) {
        showBottomUIMenu(activity);
    }

    /**
     * 显示虚拟按键
     */
    private void showBottomUIMenu(Activity activity) {
        // 显示状态栏
        activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }


    /**
     * 隐藏虚拟按键，并且全屏
     */
    private void hideBottomUIMenu(Activity activity) {
        //隐藏虚拟按键
//        View decorView = activity.getWindow().getDecorView();
//        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
//                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
//        decorView.setSystemUiVisibility(uiOptions);
        // 隐藏状态栏
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    /**
     * 检查是否存在虚拟按键栏
     *
     * @param context
     * @return
     */
    public boolean hasNavBar(Context context) {
        Resources res = context.getResources();
        int resourceId = res.getIdentifier("config_showNavigationBar", "bool", "android");
        if (resourceId != 0) {
            boolean hasNav = res.getBoolean(resourceId);
            // check override flag
            String sNavBarOverride = getNavBarOverride();
            if ("1".equals(sNavBarOverride)) {
                hasNav = false;
            } else if ("0".equals(sNavBarOverride)) {
                hasNav = true;
            }
            return hasNav;
        } else { // fallback
            return !ViewConfiguration.get(context).hasPermanentMenuKey();
        }
    }

    /**
     * 判断虚拟按键栏是否重写
     *
     * @return
     */
    @SuppressWarnings("unchecked")
    private String getNavBarOverride() {
        String sNavBarOverride = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            try {
                @SuppressWarnings("rawtypes") @SuppressLint("PrivateApi") Class c = Class.forName("android.os.SystemProperties");
                Method m = c.getDeclaredMethod("get", String.class);
                m.setAccessible(true);
                sNavBarOverride = (String) m.invoke(null, "qemu.hw.mainkeys");
            } catch (Throwable ignored) {
            }
        }
        return sNavBarOverride;
    }

}
